package com.example.demo.controller;

import com.example.demo.model.Student;
import com.example.demo.service.StudentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(tags = "学生管理相关接口")
public class StudentController {
    @Autowired
    StudentService studentService;
    @GetMapping("/AllStudents")
    @ApiOperation("查询所有学生")
    @CrossOrigin(origins = "http://localhost:8080")
    public List<Student> getAllStudents(){return studentService.list();}

    @GetMapping("/student/{id}")
    @ApiOperation("根据id查询学生")
    public Student getStudentById(@PathVariable("id") Integer id){
        Student student = studentService.getById(id);
        return student;
    }

    @PostMapping("/student")
    @ApiOperation("添加学生")
    public String addStudent(@RequestBody Student student){
        if (studentService.save(student)){
            return "插入成功";
        }
        else return "插入失败";
    }

    @PostMapping("/student")
    @ApiOperation("更新学生")
    public String updateStudent(@RequestBody Student student){
        if (studentService.updateById(student)){
            return "更新成功";
        }
        else return "更新失败";
    }

    @DeleteMapping("/user")
    @ApiOperation(("删除学生"))
    public String delStudentById(Integer id){
        if (studentService.removeById(id)){
            return "删除成功";
        }
        else return "删除失败";
    }
}
